package com.example.screenprojection;

import android.util.Log;

import java.io.IOException;
import java.net.InetAddress;

import javax.jmdns.JmDNS;
import javax.jmdns.ServiceEvent;
import javax.jmdns.ServiceListener;

public class MdnsService {

private JmDNS jmDNS;

public void startMdnsService () {
        new Thread(new Runnable() {
                @Override
                public void run () {
                        try {
                                // 初始化 JmDNS
                                jmDNS = JmDNS.create(InetAddress.getByName("0.0.0.0"));

                                // 查找设备（例如查找所有的 _http._tcp.local 服务）
                                jmDNS.addServiceListener("_http._tcp.local.", new MyServiceListener());
                        } catch (IOException e) {
                                e.printStackTrace();
                        }
                }
        }).start();
}

private class MyServiceListener implements ServiceListener {
        @Override
        public void serviceAdded (ServiceEvent event) {
                Log.d("MDNS", "Service added: " + event.getInfo());
                // 处理发现的设备或服务
        }

        @Override
        public void serviceRemoved (ServiceEvent event) {
                Log.d("MDNS", "Service removed: " + event.getInfo());
        }

        @Override
        public void serviceResolved (ServiceEvent event) {
                Log.d("MDNS", "Service resolved: " + event.getInfo());
                // 处理解析后的服务信息
        }
}
}
